/* -*- c++ -*- */

#ifndef _SCALAR_H_
#define _SCALAR_H_

#include <stdlib.h>
#include <string.h>

class Scalars {
 public:
  Scalars(): _name( NULL), _scalars( NULL), _n_scalars( 0) {}
  Scalars( const char *name, size_t n): _name( NULL), _scalars( NULL), _n_scalars( 0) {
    set_name( name);
    set( n);
  }
  ~Scalars() {
    set_name( NULL);
    set( 0);
  }

  size_t cuantos() const { return _n_scalars;}
  const char *get_name() const { return _name;}

  void set_name( const char *name);
  void set( size_t n);
  void set_scalars( const float *lst) { 
    memcpy( ( float *)_scalars, lst, _n_scalars * sizeof( float));
  }
  void set_scalar( float val) {
    for ( size_t i = 0; i < _n_scalars; i++) {
      _scalars[ i] = val;
    }
  }

  float &operator[]( int idx) { return _scalars[ idx];}

  operator const float *() const { return ( const float *)_scalars; }
  operator float *() { return ( float *)_scalars; }

  size_t memory_size() const {
    size_t ret = sizeof( *this);
    ret += _n_scalars * sizeof( float);
    ret += _name ? strlen( _name) : 0;
    return ret;
  }
  
 private:
  char *_name;
  float *_scalars;
  size_t _n_scalars;
};

inline void Scalars::set_name( const char *name) {
  if ( _name) {
    free( _name);
    _name = NULL;
  }
  if ( name) {
    _name = strdup( name);
  }
}

inline void Scalars::set( size_t n) {
  if ( _scalars) {
    delete[] _scalars;
    _scalars = NULL;
    _n_scalars = 0;
  }
  if ( n) {
    _scalars = new float[ n];
    _n_scalars = n;
  }
}

#endif // _SCALAR_H
